import axios from "axios";
import {
    Message
} from "element-ui";

const baseURL = process.env.VUE_APP_API_BASE
const req = axios.create({
    baseURL: baseURL,
    timeout: 300000,
    responseType: "json",
    headers: {
        "Content-Type": "application/json",
    },
    withCredentials: true,
    crossDomain: true,
});

/**
 *添加请求拦截器
 */
req.interceptors.request.use(function(config) {
    return config;
}, function(error) {
    return Promise.reject(error);
});
/**
 * 添加响应拦截器
 */
req.interceptors.response.use(res => {
    const {
        status,
        message,
    } = res.data;

    if (status) {
        return res.data;
    } else {
        Message({
            type: "warning",
            message: message || "网络异常",
            duration: 3000
        })
        const error = res.data;
        return Promise.reject(error);
    }
}, err => {
    let message = "网络异常";
    if (err.res && err.res.data) message = err.res.data.message;
    Message({
        type: "error",
        message: message || "网络异常",
        duration: 3000
    })
    return Promise.reject(err);
});

export default req;

export {
    baseURL
}
